-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[CRYPTO-169] Add protection for the ExceptionInInitializerError
scenario to the CryptoRandomFactory#getCryptoRandom(Properties)
method
#258
Conversation
8543907
to
8d82c96
Compare
8d82c96
to
e0fd653
Compare
Codecov Report
@@ Coverage Diff @@
## master #258 +/- ##
============================================
- Coverage 74.69% 74.66% -0.03%
Complexity 441 441
============================================
Files 38 38
Lines 1810 1816 +6
Branches 176 177 +1
============================================
+ Hits 1352 1356 +4
- Misses 348 349 +1
- Partials 110 111 +1
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
9fb8fd2
to
5a38163
Compare
@@ -141,4 +141,14 @@ public void testNull() { | |||
assertThrows(NullPointerException.class, () -> CryptoRandomFactory.getCryptoRandom(null)); | |||
} | |||
|
|||
@Test | |||
public void testExceptionInInitializerErrorRandom() throws GeneralSecurityException, IOException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there are no chagnes to CryptoRandomFactory.java
, the new test case will fail:
java.lang.ExceptionInInitializerError
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:534)
at java.base/java.lang.Class.forName(Class.java:513)
at org.apache.commons.crypto.utils.ReflectionUtils.getClassByNameOrNull(ReflectionUtils.java:93)
at org.apache.commons.crypto.utils.ReflectionUtils.getClassByName(ReflectionUtils.java:64)
at org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:189)
at org.apache.commons.crypto.random.CryptoRandomFactoryTest.testExceptionInInitializerErrorRandom(CryptoRandomFactoryTest.java:150)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
Caused by: java.lang.IllegalStateException: java.security.GeneralSecurityException: ExceptionInInitializerErrorRandom init failed
at org.apache.commons.crypto.random.ExceptionInInitializerErrorRandom.<clinit>(ExceptionInInitializerErrorRandom.java:28)
... 10 more
Caused by: java.security.GeneralSecurityException: ExceptionInInitializerErrorRandom init failed
at org.apache.commons.crypto.random.ExceptionInInitializerErrorRandom.check(ExceptionInInitializerErrorRandom.java:33)
at org.apache.commons.crypto.random.ExceptionInInitializerErrorRandom.<clinit>(ExceptionInInitializerErrorRandom.java:26)
... 10 more
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
|
||
public class ExceptionInInitializerErrorRandom implements CryptoRandom { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class is used to simulate scenarios where OpenSslCryptoRandom
fails in the static code block checkNative()
or !OpenSslCryptoRandomNative.nextRandBytes(new byte[1])
is false.
JIRA ticket: https://issues.apache.org/jira/browse/CRYPTO-169 |
Thanks @garydgregory ~ |
…rio to the CryptoRandomFactory#getCryptoRandom(Properties) method #258
YW! 😄 |
In version 1.2.0 of
commons-crypto
, the static initialization code block ofo.a.c.crypto.random.OpenSslCryptoRandom
may throw aGeneralSecurityException
wrapped byIllegalStateException
, which ultimately gets wrapped intoj.l.ExceptionInInitializerError
by Java's own mechanism.This results in behavior differences when running the statement
on platforms that do not support
OpenSslCryptoRandom
compared to when usingcommons-crypto
1.1.0 (e.g. Apple Silicon)commons-crypto
1.1.0After
OpenSslCryptoRandom
initialization fails, it tries to initializeJavaCryptoRandom
, andJavaCryptoRandom
can be successfully initialized and return results.commons-crypto
1.2.0After
OpenSslCryptoRandom
initialization fails, it throws anExceptionInInitializerError
. Since theCryptoRandomFactory#getCryptoRandom
method does not catchExceptionInInitializerError
and perform fault tolerance,ExceptionInInitializerError
continues to be thrown upward, losing the opportunity to try to initializeJavaCryptoRandom
.Therefore, this PR adds the catch and fault tolerance for
ExceptionInInitializerError
in theCryptoRandomFactory#getCryptoRandom
method to keep it behaving similarly tocommons-crypto
1.1.0.